package com.dancun.controller;


import com.dancun.entity.CTask;
import com.dancun.entity.Student;
import com.dancun.entity.TDocument;
import com.dancun.entity.TGroup;
import com.dancun.service.ICTaskService;
import com.dancun.service.ITGroupService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-10-02
 */
@RestController
public class CTaskController {


    @Autowired
    ICTaskService cTaskService;

    @Autowired
    ITGroupService tGroupService;

    @RequestMapping("/listTask")
    public ModelAndView list() {
        ModelAndView mav = new ModelAndView("/Main/TaskList.jsp");
        List<CTask> task = cTaskService.listt(null);
        mav.addObject("list", task);
        return mav;
    }

    @RequestMapping(value = "/findtask")
    public ModelAndView find(@Param("id") String id) {
        ModelAndView mav = new ModelAndView("/Main/TaskUpdate.jsp");
        CTask task = cTaskService.getById(id);
        mav.addObject("task", task);
        return mav;
    }

    @RequestMapping("/updatetask")
    public ModelAndView update(CTask task) {
        ModelAndView mav = new ModelAndView("/Tasklist");
        if (cTaskService.updateById(task)) {
            return mav;
        }
        mav.setViewName("/Tasklist");
        return mav;
    }

    @RequestMapping("/Tasknew")
    public ModelAndView list1() {
        ModelAndView mav = new ModelAndView("/Main/TaskNew.jsp");
        List<CTask> task = cTaskService.list();
        mav.addObject("list", task);
        return mav;
    }


    @RequestMapping("/taskAdd")
    public String save(CTask cTask, @Param("date1") Long date1, @Param("data2") Long date2, HttpServletResponse response) throws IOException {
        cTask.setStartDate(LocalDateTime.now());
        cTask.setEndDate(LocalDateTime.now());
        cTask.setTaskId((new SimpleDateFormat("yyMMddhhmmss")).format(new Date()) + (int) Math.random() * 100);
        ModelAndView mav = null;
        cTask.setStartDate(LocalDateTime.ofEpochSecond(date1, 0, ZoneOffset.ofHours(8)));
        cTask.setEndDate(LocalDateTime.ofEpochSecond(date2, 0, ZoneOffset.ofHours(8)));


        if (cTaskService.save(cTask)) {
            response.sendRedirect("listTask");

        } else {
            response.sendRedirect("listTask");
        }
        return null;
    }

    @RequestMapping("/finTaskJson")
    public String findTask(CTask cTask) throws IOException {
        CTask t = cTaskService.getById(cTask.getTaskId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("tname", t.getTname());
        jsonObject.put("startDate", t.getStartDate().toString());
        jsonObject.put("endDate", t.getEndDate().toString());
        jsonObject.put("content", t.getContent());


        return jsonObject.toString();
    }

    @RequestMapping("/listTaskByStu")
    public String listTaskByStu(String stno) throws IOException {

        List<TGroup> list = tGroupService.listByName(stno);

        JSONArray jsonArray = new JSONArray();
        for (TGroup t : list) {
            try{
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("groupId", t.getGroupId());
                try {

                    jsonObject.put("stno1", t.getStno1());
                    jsonObject.put("stno2", t.getStno2());
                    jsonObject.put("stno3", t.getStno3());
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
                JSONObject task = null;
                CTask cTask = cTaskService.getById(t.getTaskId());

                task = JSONObject.fromObject( cTask);
                task.put("endDate", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(cTask.getEndDate()));
                task.put("startDate", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(cTask.getStartDate()));
                jsonObject.put("task", task);
                jsonArray.add(jsonObject);
            }catch (Exception e){
                e.printStackTrace();
            }



        }
        JSONObject js = new JSONObject();
        js.put("date", jsonArray);


        return js.toString();
    }

    @GetMapping("/taskdoc")
    public ModelAndView findOneDoc(String id) {
        ModelAndView mav = new ModelAndView("/Main/task.jsp");
        CTask task = cTaskService.getById(id);
        mav.addObject("task", task);
        return mav;
    }

    /**
     * 实训任务表
     *
     * @return
     */
    @RequestMapping("/Tasklist")
    public ModelAndView Tasklist() {
        ModelAndView mav = new ModelAndView("/Main/M_TaskList.jsp");
        List<CTask> task = cTaskService.listt(null);
        mav.addObject("list", task);
        return mav;
    }

    @RequestMapping("/delCTask")
    public String delete(@Param("id") String taskId, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        if (cTaskService.removeById(taskId)) {
            out.print("<script>alert('删除成功');window.location.href='Tasklist'</script>");
        } else {
            out.print("<script>alert('删除失败');window.location.href='Tasklist'</script>");
        }
        return null;

    }


}
